/**
 * 
 */
$.fn.s_control=function(control){
    var element=$(this);
    control=$(control);
    /*element.delegate("li","click",function(){
        var tabName=$(this).attr("data-tab");
         //点击li的时候触发change.tabs自定义事件  
        element.trigger("change.tabs",tabName); 
    });*/
         
    //给element绑定一个change.tabs自定义事件
    element.on("valueChange",function(event,newId,newName){
    	var option = event.data;
    	var old_id = element.attr("data-id");
    	if (StrUtil.strnull(old_id) != StrUtil.strnull(newId)){
    		/*control.val("");
    		control.attr("data-id","");
    		control.attr("data-name","");*/
    		if (control != null && control.length > 0){
    			if (control.attr("id") == "s_building"){
        			control.trigger("refresh",newId);
        		}else{
        			control.trigger("valueChange",["",""]);
        		}
    		}
    		
    	}
    	element.val(newName);
    	element.attr("data-id",newId);
    	element.attr("data-name",newName);
    });     
    return this;
};
function s_show(){
	$("#queryModal").modal({backdrop: 'static', keyboard: false});
}
function s_init(initObj) {
	var i_company = initObj.company;
	var i_community = initObj.community;
	var i_building = initObj.building;
	var handler = initObj.handler;
	if (i_building.isNeed == true){
		$("#s_building").attr("isNeed","1");
		//选择楼栋
		$("#sd_company").css('display','block');
		$("#sd_community").css('display','block');
		$("#sd_building").css('display','block');
		$("#s_company").s_control("#s_community");
		$("#s_community").s_control("#s_building");
		$("#s_building").selectDiv("val",{communityId:$('#s_community').attr("data-id")});
	}else if (i_community.isNeed == true){
		$("#s_community").attr("isNeed","1");
		//选择到小区
		$("#sd_company").css('display','block');
		$("#sd_community").css('display','block');
		$("#s_company").s_control("#s_community");
		$("#s_community").s_control("");
	}else if(i_company.isNeed == true){
		$("#s_company").attr("isNeed","1");
		//选择到物业公司
		$("#s_company").s_control("");
		$("#sd_company").css('display','block');
	}
	
	if (!StrUtil.isEmpty(i_company.companyId)){
		$('#s_company').trigger("valueChange",[i_company.companyId,i_company.companyName]);
	}
	if (!StrUtil.isEmpty(i_community.communityId)){
		$('#s_community').trigger("valueChange",[i_community.communityId,i_community.communityName]);
	}
	
	$("#s_submit_btn").on("click",function(){
		var retObj ={company:{},community:{},building:{},unit:{},room:{}};
		retObj.company.companyId=$('#s_company').attr("data-id");
		retObj.company.companyName=$('#s_company').attr("data-name");
		retObj.community.communityName=$('#s_community').attr("data-name");
		retObj.community.communityId=$('#s_community').attr("data-id");
		retObj.building.buildingId=$('#s_building').data("buildingId");
		retObj.building.buildingName=$('#s_building').data("buildingName");
		retObj.unit.unitId=$('#s_building').data("unitId");
		retObj.unit.unitName=$('#s_building').data("unitName");
		retObj.room.roomId=$('#s_building').data("roomId");
		retObj.room.roomNbr=$('#s_building').data("roomNbr");
		
		if ('1' == $("#s_building").attr("isNeed")){
			if (StrUtil.isEmpty($('#s_building').data("buildingId"))){
				$.sucmodal("提示", "请选择楼栋");
				return;
			}
		}else if('1' == $("#s_community").attr("isNeed")){
			if (StrUtil.isEmpty($('#s_community').data("communityId"))){
				$.sucmodal("提示", "请选择小区");
				return;
			}
		}else if('1' == $("#s_company").attr("isNeed")){
			if (StrUtil.isEmpty($('#s_company').data("companyId"))){
				$.sucmodal("提示", "请选择物业公司");
				return;
			}
		}
		handler(retObj);
		
		$("#queryModal").modal("hide");
	});
	/**
backdrop 为 static 时，点击模态对话框的外部区域不会将其关闭。
keyboard 为 false 时，按下 Esc 键不会关闭 Modal。
	 */
	//$("#queryModal").modal({backdrop: 'static', keyboard: false});
	// 物业公司查询
	$('#s_companySelect').on('show.bs.modal', function() {
		$("#queryModal").modal('hide');
	});
	$('#s_companySelect').on('hide.bs.modal', function() {
		$("#queryModal").modal('show');
	});
	$("#s_company").on("click", function() {
		$("#s_companySelect").modal();
	});
	/*$('#s_company').on("valuechange",function(){
		$('#s_community').val("");
		$('#s_community').attr("data-id","");
		$('#s_community').attr("data-name","");
	});*/
	// 小区查询
	$('#s_communitySelect').on('show.bs.modal', function() {
		$("#queryModal").modal('hide');
	});
	$('#s_communitySelect').on('hide.bs.modal', function() {
		$("#queryModal").modal('show');
	});
	$("#s_community").on("click", function() {
		var companyId = $('#s_company').attr("data-id");
		if (!StrUtil.isEmpty(companyId)) {
			$("#s_communitySelect").modal();
		}
	});
	
	//楼栋/单元/房间
	/*$('#s_buildingSelect').on('show.bs.modal', function() {
		$("#queryModal").modal('hide');
	});
	$('#s_buildingSelect').on('hide.bs.modal', function() {
		$("#queryModal").modal('show');
	});*/
	
	/*$("#s_building").on("click", function() {
		var communityId = $('#s_community').attr("data-id");
		if (!StrUtil.isEmpty(companyId)) {
			$("#s_building").selectDiv("val",{communityId:communityId});
		}
	});*/
}

function companySearch() {

	var propertyCompanyName = $("#s_companySelect > input[name=keywords]")
			.val();
	var jsondata = {};
	jsondata.propertyCompanyName = propertyCompanyName;
	$
			.post(
					"propertyCompanyAction!getPropertyCompanyList.action",
					{
						jsondata : JSON.stringify(jsondata)
					},
					function(data) {
						data = eval("(" + data + ")");
						if (data != null && data.result == "true") {
							$("#demo")
									.myPagination(
											{
												currPage : 1,
												pageSize : 5,
												pageCount : data.pageInfo.totalPageCount,
												ajax : {
													on : true, // 开启状态
													// url:
													// "mbook!getMBookByCondition.action?type="+type+"&keyword="+keyword,
													// //访问服务器地址
													url : "propertyCompanyAction!getPropertyCompanyList.action",
													dataType : 'json', // 返回类型
													callback : 'companyCallBack',
													param : {
														"on" : true,
														jsondata : JSON
																.stringify(jsondata)
													},
													ajaxStart : function() {
														return false;
													},
													onClick : function(page) {
														alert(page);
													}
												}
											});
						} else {
							alert("查询失败:" + data.msg);
						}

					});
}

function companyCallBack(data) {
	$("#s_companyTable>tbody").children().remove();
	data = eval("(" + data + ")");
	$(data.pageInfo.dataList)
			.each(
					function(ix, ite) {
						var trStr = '<tr data-id="'
								+ ite.propertyCompany.companyId
								+ '" data-name="'
								+ ite.propertyCompany.companyName + '">';
						// trStr += '<td class="with-checkbox"
						// style="display:none"><input type="checkbox"
						// name="check"
						// value="'+ite.propertyCompany.companyId+'" /></td>';
						trStr += '<td>' + ite.propertyCompany.companyName
								+ '</td>';
						if (ite.propertyCompany != null
								&& ite.propertyCompany.companyInfo != ""
								&& ite.propertyCompany.companyInfo.officePhone != null
								&& ite.propertyCompany.companyInfo.officePhone != "") {
							trStr += '<td>'
									+ ite.propertyCompany.companyInfo.officePhone
									+ '</td>';
						} else {
							trStr += '<td>' + "" + '</td>';
						}
						if (!StrUtil.isEmpty(ite.propertyCompany.partyInfo)
								&& ite.propertyCompany.partyInfo != "") {
							trStr += '<td>'
									+ StrUtil
											.strnull(ite.propertyCompany.partyInfo.partyName)
									+ '</td>';
							trStr += '<td>'
									+ StrUtil
											.strnull(ite.propertyCompany.partyInfo.mobile)
									+ '</td>';
						} else {
							trStr += '<td>' + "" + '</td>';
							trStr += '<td>' + "" + '</td>';
						}
						trStr += '<td>' + ite.propertyCompany.statusName
								+ '</td>';
						/*
						 * if (ite.propertyCompany.statusCd == '1000') { trStr += '<td>有效</td>'; }
						 * else { trStr += '<td>失效</td>'; }
						 */
						// trStr +='<td>';
						// trStr +='<a
						// href="propertyCompanyInfo.jsp?companyId='+ite.propertyCompany.companyId+'
						// " class="btn defined-role-btn" >详情</a></td>';
						trStr += '</tr>';
						$("#s_companyTable>tbody").append(trStr);
					});
	$("#s_companyTable > tbody > tr").click(function() {
		$('#s_companySelect').modal('hide');
		$('#s_company').trigger("valueChange",[$(this).attr("data-id"),$(this).attr("data-name")]);
		/*$('#s_company').val($(this).attr("data-name"));
		$('#s_company').attr("data-id", $(this).attr("data-id"));
		$('#s_company').attr("data-name", $(this).attr("data-name"));*/
	});
}

function communitySearch() {

	var communityName = $("#s_communitySelect > input[name=keywords]").val();
	var companyId = $('#s_company').attr("data-id");
	$.post("communityAction!getCommunity.action", {
		inParma : JSON.stringify({
			"communityName" : communityName,
			"companyId" : companyId
		})
	}, function(data) {
		data = eval("(" + data + ")");
		$("#demo").myPagination({
			currPage : 1,
			pageSize : 5,
			pageCount : data.pageCount,
			ajax : {
				on : true, // 开启状态
				// url:
				// "mbook!getMBookByCondition.action?type="+type+"&keyword="+keyword,
				// //访问服务器地址
				url : "communityAction!getCommunity.action",
				dataType : 'json', // 返回类型
				callback : 'communityCallBack',
				param : {
					"on" : true,
					inParma : JSON.stringify({
						"communityName" : communityName,
						"companyId" : companyId
					})
				},
				ajaxStart : function() {
					return false;
				},
				onClick : function(page) {
					alert(page);
				}
			}
		});
	});
}

function communityCallBack(data) {
	$("#s_communityTable>tbody").children().remove();
	data = eval("(" + data + ")");
	if (data.result != "true") {
		alert("请求失败" + data.msg);
	} else {
		$(data.data).each(
				function(ix, ite) {
					var trStr = '<tr data-id="' + ite.communityId
							+ '" data-name="' + ite.communityName + '">';
					/*
					 * trStr += '<td class="with-checkbox"><input
					 * type="checkbox" name="check" value="' + ite.communityId + '" /></td>';
					 */
					if (ite.propertyCompany != null) {
						trStr += '<td>' + ite.propertyCompany.companyName
								+ '</td>';
					} else {
						trStr += '<td></td>';
					}

					trStr += '<td>' + ite.communityName + '</td>';
					if (ite.communityInfo != null) {
						trStr += '<td>' + ite.communityInfo.officePhone
								+ '</td>';
					} else {
						trStr += '<td></td>';
					}
					if (!StrUtil.isEmpty(ite.partyInfo)) {
						trStr += '<td>'
								+ StrUtil.strnull(ite.partyInfo.partyName)
								+ '</td>';
						trStr += '<td>' + StrUtil.strnull(ite.partyInfo.mobile)
								+ '</td>';
					} else {
						trStr += '<td></td>';
						trStr += '<td></td>';
					}

					if (ite.statusCd == '1000') {
						trStr += '<td>有效</td>';
					} else {
						trStr += '<td>失效</td>';
					}

					trStr += '</tr>';
					$("#s_communityTable>tbody").append(trStr);
				});
		$("#s_communityTable > tbody > tr").click(function () {
			$('#s_communitySelect').modal('hide');
			$("#s_building").selectDiv("val",{communityId:$(this).attr("data-id")});
			$('#s_community').trigger("valueChange",[$(this).attr("data-id"),$(this).attr("data-name")]);
			/*$('#s_community').val($(this).attr("data-name"));
			$('#s_community').attr("data-id",$(this).attr("data-id"));
			$('#s_community').attr("data-name",$(this).attr("data-name"));*/
	    });
	}

}